// linepre.js
var app = getApp();
function checktaken(that, options) {
  if (app.accesstoken == "") {
    setTimeout(function () {
      checktaken(that, options);
    }, 500);
  } else {
    that.getqrcodeinfo(options);
  }
}
Page({

  /**
   * 页面的初始数据
   */
  data: {
    distance: "约34km",
    up: true,
    down: true,
    upSite: '',
    downSite: '',
    appColor: app.appColor
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    app.showLoadings();
    var that = this;
    // 中途上车二维码购票
    if (options.scancode && options.scancode != undefined && typeof (options.scancode) != null) {
      checktaken(that, options);
    }
    // 如果不是通过扫码购票
    else {
      this.price = options.price;
      this.data.time = options.starttime;
      this.setData({
        starttime: options.starttime,
        price: options.price,
        date: options.date,
        ticketnum: options.surplus,
        busid: options.busid
      })
      wx.getStorage({
        key: 'getOn',
        success: function (res) {
          that.setData({
            startList: res.data,
            onid: res.data.getOnID,
            offid: res.data.getOffID,
            onLocation: res.data.onLocation,
            offLocation: res.data.offLocation
          })
        },
      });
    }

  },
  getqrcodeinfo(options) {
    var that = this;
    wx.request({
      url: app.serviceurl + 'CustomLine/CustomQueryBusByQRCode/',
      header: {
        Authorization: "Bearer " + app.accesstoken
      },
      data: {
        QRCodeInfo: options.scancode
      },
      dataType: 'json',
      success: function (res) {
        app.IsScanCOde = true;
        let startList = {};
        startList.getOn = [];
        startList.getOnID = [];
        startList.onLocation = [];
        startList.getOff = [];
        startList.getOffID = [];
        startList.offLocation = [];
        if (typeof (res.data.TakePoints) != undefined && res.data.TakePoints != null) {
          res.data.TakePoints.forEach(function (item, i) {
            if (item.IsUpPoint) {
              startList.getOn.push(item.PointName);
              startList.getOnID.push(item.PointID);
              startList.onLocation.push(item.Coordinate);
            } else {
              startList.getOff.push(item.PointName);
              startList.getOffID.push(item.PointID);
              startList.offLocation.push(item.Coordinate);
            }
          })
          that.setData({
            isScanCode: true,
            startList: startList,
            onLocation: startList.onLocation,
            offLocation: startList.offLocation,
            onid: startList.getOnID,
            offid: startList.getOffID,
            time: res.data.BusPlans.StartTime,
            price: res.data.BusPlans.TicketPrice,
            date: res.data.BusPlans.StartDate
          })
        }
        app.line.BusPlanId = res.data.BusPlans.BusLineID;
      },
      fail: function () {
        console.log("调用失败");
      }
    })
  },
  location: function (e) {
    try{
      var stand = e.currentTarget.dataset.stand,
        location = e.currentTarget.dataset.location.split(',');
      wx.openLocation({
        latitude: parseFloat(location[1]),
        longitude: parseFloat(location[0]),
        scale: 14,
        name: stand,
      })
    }catch(err){
      console.log(err)
    }
  },

  getOnID: function (e) {
    app.line.upPointId = e.currentTarget.dataset.onid;
    this.setData({
      upSite: e.currentTarget.dataset.up,
      up: false
    });
  },

  getOffID: function (e) {
    app.line.downPointId = e.currentTarget.dataset.offid;
    this.setData({
      downSite: e.currentTarget.dataset.down,
      down: false
    })
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    app.hideLoadings();
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (res) {
    if (res.from === 'menu') {
      return app.OnShare();
    }
  },

  // 提示框
  AlertName: function () {
    wx.showModal({
      content: '请选择上车下车地点！',
      showCancel: false,
      success: function (res) {
        if (res.confirm) {
          console.log('确定')
        }
      }
    });
  },
  buy: function (e) {
    var that = this;
    if (this.data.upSite != '' && this.data.downSite != '') {
      wx.redirectTo({
        url: '../confirm/confirm?price=' + that.data.price + '&time=' + that.data.time + '&upSite=' + that.data.upSite + '&downSite=' + that.data.downSite + '&date=' + that.data.date
      });
    }
    else {
      this.AlertName();
    }
  },

  guide: function () {
    wx.navigateTo({
      url: './guide/guide',
    })
  },
  help: function (e) {
    wx.navigateTo({
      url: '../confirm/confirm',
    })
  },

})